<template>
  <div class="history-container">
    <!-- 顶部导航栏 -->
    <div class="navbar glass-effect">
      <div class="navbar-content">
        <div class="brand">
          <svg viewBox="0 0 40 40" class="brand-logo">
            <defs>
              <linearGradient id="logoGradient" x1="0%" y1="0%" x2="100%" y2="100%">
                <stop offset="0%" style="stop-color:#ec4899;stop-opacity:1" />
                <stop offset="50%" style="stop-color:#d946ef;stop-opacity:1" />
                <stop offset="100%" style="stop-color:#8b5cf6;stop-opacity:1" />
              </linearGradient>
            </defs>
            <path d="M20 4 L36 12 L36 28 L20 36 L4 28 L4 12 Z" 
                  fill="url(#logoGradient)" stroke="white" stroke-width="1"/>
            <circle cx="20" cy="16" r="3" fill="white"/>
            <rect x="17" y="20" width="6" height="12" rx="1" fill="white"/>
          </svg>
          <h1 class="brand-name gradient-text">历史记录</h1>
        </div>
        <div class="nav-actions">
          <el-button 
            @click="$router.push('/tryon')" 
            type="primary"
            :icon="MagicStick"
            class="nav-btn"
          >
            返回试衣
          </el-button>
          <el-button 
            type="danger" 
            @click="handleLogout"
            :icon="SwitchButton"
            class="nav-btn"
          >
            退出登录
          </el-button>
        </div>
      </div>
    </div>
    
    <!-- 主内容区域 -->
    <div class="main-content">
      <div class="content-wrapper">
        <!-- 页面标题 -->
        <div class="page-header animate-fade-in-up">
          <div class="header-icon">
            <el-icon><Clock /></el-icon>
          </div>
          <div class="header-text">
            <h2 class="header-title">我的试衣记录</h2>
            <p class="header-subtitle">浏览您的所有 AI 试衣历史</p>
          </div>
          <div class="header-stats">
            <div class="stat-card">
              <div class="stat-number">{{ historyList.length }}</div>
              <div class="stat-label">总记录</div>
            </div>
          </div>
        </div>
        
        <!-- 空状态 -->
        <div v-if="historyList.length === 0" class="empty-state animate-fade-in-up">
          <div class="empty-illustration">
            <div class="illustration-circle">
              <el-icon class="illustration-icon"><FolderOpened /></el-icon>
            </div>
          </div>
          <h3 class="empty-title">暂无试衣记录</h3>
          <p class="empty-description">
            您还没有任何试衣历史<br/>
            快去试试 AI 智能试衣吧！
          </p>
          <el-button 
            type="primary" 
            size="large"
            @click="$router.push('/tryon')"
            class="goto-tryon-btn"
          >
            <el-icon><MagicStick /></el-icon>
            <span>开始试衣</span>
          </el-button>
        </div>
        
        <!-- 历史记录网格 -->
        <div v-else class="history-grid">
          <div 
            v-for="(item, index) in historyList" 
            :key="item.id" 
            class="history-card animate-fade-in-up card-hover"
            :style="{ animationDelay: `${index * 0.1}s` }"
          >
            <div class="card-image-wrapper">
              <img :src="item.resultUrl" class="card-image" alt="试衣效果" />
              <div class="image-overlay">
                <el-button 
                  type="primary" 
                  :icon="View"
                  circle
                  size="large"
                  @click="viewDetail(item)"
                />
              </div>
              <div class="image-badge">
                <el-icon><Check /></el-icon>
              </div>
            </div>
            
            <div class="card-content">
              <div class="card-meta">
                <el-icon class="meta-icon"><Clock /></el-icon>
                <span class="meta-text">{{ item.date }}</span>
              </div>
              
              <div class="card-actions">
                <el-button 
                  size="small" 
                  :icon="Download"
                  @click="downloadItem(item)"
                  class="action-button"
                >
                  下载
                </el-button>
                <el-button 
                  size="small" 
                  :icon="Share"
                  @click="shareItem(item)"
                  class="action-button"
                >
                  分享
                </el-button>
                <el-button 
                  size="small" 
                  type="danger"
                  :icon="Delete"
                  @click="deleteItem(item.id)"
                  class="action-button"
                >
                  删除
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 底部导航 -->
    <BottomNav />
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Clock, 
  MagicStick, 
  SwitchButton,
  FolderOpened,
  View,
  Download,
  Share,
  Delete,
  Check
} from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'
import api from '@/composables/useApi'
import BottomNav from '@/components/BottomNav.vue'

// @generatedBy AI-CodeGen
// @lastModified 2025-10-27

const router = useRouter()
const authStore = useAuthStore()

const historyList = ref([])
const loading = ref(false)

// 用户ID
const userId = computed(() => authStore.user?.id || 1)

const viewDetail = (item) => {
  ElMessageBox.alert(
    `<div style="text-align: center;">
      <img src="${item.resultUrl}" style="max-width: 100%; border-radius: 12px;" />
      <p style="margin-top: 1rem; color: #64748b;">创建时间: ${item.date}</p>
    </div>`,
    '试衣详情',
    {
      dangerouslyUseHTMLString: true,
      confirmButtonText: '关闭',
      center: true
    }
  )
}

const downloadItem = (item) => {
  // TODO: 实现下载功能
  ElMessage.success('下载成功！')
}

const shareItem = (item) => {
  // TODO: 实现分享功能
  ElMessage.info('分享功能开发中，敬请期待 🚀')
}

const deleteItem = async (id) => {
  try {
    await ElMessageBox.confirm(
      '确定要删除这条记录吗？删除后无法恢复。',
      '确认删除',
      {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    
    // 调用删除接口
    await api.delete(`/tryon/history/${id}`)
    
    // 从列表中移除
    historyList.value = historyList.value.filter(item => item.id !== id)
    ElMessage.success('删除成功！')
  } catch (error) {
    if (error !== 'cancel') {
      console.error('删除失败:', error)
      ElMessage.error('删除失败')
    }
  }
}

const handleLogout = () => {
  authStore.logout()
  router.push('/login')
}

// 加载历史记录
const loadHistory = async () => {
  loading.value = true
  try {
    const response = await api.get('/tryon/history', {
      params: { userId: userId.value }
    })
    
    // 格式化数据
    historyList.value = (response.data.history || []).map(item => ({
      id: item.id,
      personUrl: item.personImageUrl,
      garmentUrl: item.garmentImageUrl,
      resultUrl: item.resultImageUrl,
      date: formatDate(item.createdAt),
      garmentType: item.garmentType
    }))
    
    console.log('历史记录加载成功:', historyList.value)
  } catch (error) {
    console.error('加载历史记录失败:', error)
    ElMessage.error('加载历史记录失败')
  } finally {
    loading.value = false
  }
}

// 格式化日期
const formatDate = (dateStr) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hour = String(date.getHours()).padStart(2, '0')
  const minute = String(date.getMinutes()).padStart(2, '0')
  return `${year}-${month}-${day} ${hour}:${minute}`
}

onMounted(() => {
  loadHistory()
})
</script>

<style scoped>
.history-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #fdf2f8 0%, #fae8ff 50%, #f3e8ff 100%);
}

/* 导航栏 */
.navbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(217, 70, 239, 0.1);
  box-shadow: 0 4px 20px rgba(217, 70, 239, 0.08);
}

.navbar-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 1rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.brand {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.brand-logo {
  width: 40px;
  height: 40px;
  filter: drop-shadow(0 2px 8px rgba(217, 70, 239, 0.3));
}

.brand-name {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0;
}

.nav-actions {
  display: flex;
  gap: 1rem;
}

.nav-btn {
  border-radius: 10px;
}

/* 主内容区域 */
.main-content {
  padding: 2rem;
}

.content-wrapper {
  max-width: 1400px;
  margin: 0 auto;
}

/* 页面头部 */
.page-header {
  display: flex;
  align-items: center;
  gap: 2rem;
  background: white;
  border-radius: 24px;
  padding: 2rem;
  margin-bottom: 2rem;
  box-shadow: 0 10px 40px rgba(217, 70, 239, 0.1);
  border: 1px solid rgba(217, 70, 239, 0.1);
}

.header-icon {
  width: 80px;
  height: 80px;
  border-radius: 20px;
  background: linear-gradient(135deg, #ec4899, #d946ef, #8b5cf6);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 2.5rem;
  box-shadow: 0 8px 24px rgba(217, 70, 239, 0.3);
}

.header-text {
  flex: 1;
}

.header-title {
  font-size: 2rem;
  font-weight: 700;
  background: linear-gradient(135deg, #ec4899, #d946ef, #8b5cf6);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin: 0 0 0.5rem 0;
}

.header-subtitle {
  color: #64748b;
  margin: 0;
  font-size: 1rem;
}

.header-stats {
  display: flex;
  gap: 1rem;
}

.stat-card {
  text-align: center;
  padding: 1rem 2rem;
  background: linear-gradient(135deg, #fdf4ff, #fae8ff);
  border-radius: 16px;
  border: 1px solid #f0abfc;
  min-width: 120px;
}

.stat-number {
  font-size: 2rem;
  font-weight: 700;
  background: linear-gradient(135deg, #ec4899, #d946ef);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 0.25rem;
}

.stat-label {
  color: #64748b;
  font-size: 0.9rem;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 500px;
  text-align: center;
  padding: 3rem;
  background: white;
  border-radius: 24px;
  box-shadow: 0 10px 40px rgba(217, 70, 239, 0.1);
}

.empty-illustration {
  margin-bottom: 2rem;
}

.illustration-circle {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  background: linear-gradient(135deg, #fdf4ff, #fae8ff);
  border: 4px dashed #d946ef;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: float 3s ease-in-out infinite;
}

.illustration-icon {
  font-size: 4rem;
  color: #d946ef;
}

.empty-title {
  font-size: 2rem;
  font-weight: 700;
  color: #1e293b;
  margin: 0 0 1rem 0;
}

.empty-description {
  font-size: 1.1rem;
  line-height: 1.6;
  color: #64748b;
  margin: 0 0 2rem 0;
}

.goto-tryon-btn {
  height: 56px;
  padding: 0 2.5rem;
  font-size: 1.1rem;
  font-weight: 600;
  border-radius: 14px;
  box-shadow: 0 8px 20px rgba(217, 70, 239, 0.3);
}

/* 历史记录网格 */
.history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1.5rem;
}

.history-card {
  background: white;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(217, 70, 239, 0.08);
  border: 1px solid rgba(217, 70, 239, 0.1);
}

.card-image-wrapper {
  position: relative;
  width: 100%;
  padding-top: 133.33%; /* 3:4 aspect ratio */
  overflow: hidden;
  background: linear-gradient(135deg, #fdf4ff, #fae8ff);
}

.card-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.history-card:hover .card-image {
  transform: scale(1.05);
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.card-image-wrapper:hover .image-overlay {
  opacity: 1;
}

.image-badge {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(16, 185, 129, 0.95);
  backdrop-filter: blur(10px);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

.card-content {
  padding: 1.25rem;
}

.card-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  color: #64748b;
  font-size: 0.9rem;
}

.meta-icon {
  font-size: 1rem;
}

.card-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.action-button {
  flex: 1;
  min-width: 70px;
  border-radius: 8px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .history-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
  
  .page-header {
    flex-direction: column;
    text-align: center;
  }
  
  .header-icon {
    width: 64px;
    height: 64px;
    font-size: 2rem;
  }
  
  .navbar-content {
    padding: 1rem;
  }
}

/* 手机端优化 - 移动优先 */
@media (max-width: 640px) {
  /* 导航栏优化 */
  .navbar-content {
    padding: 0.75rem 1rem;
    flex-wrap: wrap;
  }
  
  .brand {
    gap: 0.5rem;
  }
  
  .brand-logo {
    width: 32px;
    height: 32px;
  }
  
  .brand-name {
    font-size: 1.125rem;
  }
  
  .nav-actions {
    width: 100%;
    margin-top: 0.75rem;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
  }
  
  .nav-btn {
    width: 100%;
    justify-content: center;
  }
  
  .nav-btn :deep(.el-button__text) {
    font-size: 0.875rem;
  }
  
  /* 主内容区域 */
  .main-content {
    padding: 0.75rem;
  }
  
  /* 页面头部优化 */
  .page-header {
    padding: 1.25rem;
    border-radius: 20px;
    margin-bottom: 1rem;
    gap: 1rem;
  }
  
  .header-icon {
    width: 56px;
    height: 56px;
    font-size: 1.75rem;
    border-radius: 16px;
  }
  
  .header-title {
    font-size: 1.5rem;
  }
  
  .header-subtitle {
    font-size: 0.875rem;
  }
  
  .header-stats {
    width: 100%;
    justify-content: center;
  }
  
  .stat-card {
    padding: 0.85rem 1.5rem;
    border-radius: 12px;
  }
  
  .stat-number {
    font-size: 1.75rem;
  }
  
  .stat-label {
    font-size: 0.85rem;
  }
  
  /* 空状态优化 */
  .empty-state {
    min-height: 400px;
    padding: 2rem 1.5rem;
    border-radius: 20px;
  }
  
  .illustration-circle {
    width: 110px;
    height: 110px;
  }
  
  .illustration-icon {
    font-size: 3.5rem;
  }
  
  .empty-title {
    font-size: 1.75rem;
  }
  
  .empty-description {
    font-size: 1rem;
  }
  
  .goto-tryon-btn {
    height: 52px;
    padding: 0 2rem;
    font-size: 1.05rem;
    border-radius: 12px;
  }
  
  /* 历史记录网格优化 */
  .history-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
  
  .history-card {
    border-radius: 16px;
  }
  
  .card-content {
    padding: 1rem;
  }
  
  .card-meta {
    margin-bottom: 0.85rem;
    font-size: 0.85rem;
  }
  
  .card-actions {
    gap: 0.5rem;
  }
  
  .action-button {
    min-width: 65px;
    font-size: 0.875rem;
  }
  
  .action-button :deep(.el-button__text) {
    font-size: 0.875rem;
  }
}

/* 超小屏幕优化 */
@media (max-width: 375px) {
  .navbar-content {
    padding: 0.6rem 0.75rem;
  }
  
  .brand-name {
    font-size: 1rem;
  }
  
  .page-header {
    padding: 1rem;
  }
  
  .header-icon {
    width: 48px;
    height: 48px;
    font-size: 1.5rem;
  }
  
  .header-title {
    font-size: 1.25rem;
  }
  
  .stat-card {
    padding: 0.75rem 1.25rem;
  }
  
  .stat-number {
    font-size: 1.5rem;
  }
  
  .empty-state {
    padding: 1.5rem 1rem;
  }
  
  .illustration-circle {
    width: 90px;
    height: 90px;
  }
  
  .illustration-icon {
    font-size: 3rem;
  }
  
  .empty-title {
    font-size: 1.5rem;
  }
  
  .empty-description {
    font-size: 0.95rem;
  }
  
  .goto-tryon-btn {
    height: 50px;
    padding: 0 1.75rem;
    font-size: 1rem;
  }
  
  .card-content {
    padding: 0.85rem;
  }
  
  .action-button {
    min-width: 60px;
  }
}

/* 横屏优化 */
@media (max-width: 896px) and (orientation: landscape) {
  .navbar-content {
    padding: 0.5rem 1rem;
  }
  
  .nav-actions {
    margin-top: 0;
    width: auto;
  }
  
  .page-header {
    flex-direction: row;
    padding: 1rem 1.5rem;
  }
  
  .header-icon {
    width: 48px;
    height: 48px;
    font-size: 1.5rem;
  }
  
  .header-stats {
    width: auto;
  }
  
  .empty-state {
    min-height: 300px;
    padding: 1.5rem;
  }
  
  .illustration-circle {
    width: 80px;
    height: 80px;
  }
  
  .illustration-icon {
    font-size: 2.5rem;
  }
  
  .empty-title {
    font-size: 1.5rem;
  }
  
  .history-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
